<?php

/**
 * rutas actions.
 *
 * @package    SistemaRutasEc
 * @subpackage rutas
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class rutasActions extends sfActions {

    public function executeIndex(sfWebRequest $request) {
        $this->idco = $request->getParameter('id_co');
        $this->getUser()->setAttribute('id_co', $this->idco);
        $q = Doctrine_Query::create()
                ->from('cooperativa c')
                ->where('c.id_co=?', $this->idco);
        $this->cpr = $q->fetchOne();
        
        $q2 = Doctrine_Query::create()
                ->from('regla r')
                ->where('r.id_co=?', $this->idco);
        $this->regla = $q2->fetchOne();
        
        $this->rutas = Doctrine::getTable('ruta')->createQuery('r')
                ->where('r.id_co = ?', $this->idco)
                ->execute();
        
        
    }

    public function executeShow(sfWebRequest $request) {
        $this->ruta = Doctrine_Core::getTable('ruta')->find(array($request->getParameter('id_ru')));
        $this->forward404Unless($this->ruta);
    }

    
    public function executeActivar(sfWebRequest $request) {
        // sorting
        $idru = $request->getParameter('id_ru');
        
        $rut = Doctrine::getTable('ruta')->find($idru);
        if (!($rut instanceof ruta)) {
            $this->redirect404();
        }
        $rut['activo'] = !$rut['activo'];
        $rut->save();

        $this->redirect('rutas/index?id_co='.$rut->getIdCo());
    }
    public function executeNew(sfWebRequest $request) {
        $this->icr = $request->getParameter('id_co');
        
        
        $this->form = new rutaForm();
    }

    public function executeCreate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST));

        $this->form = new rutaForm();

        $this->processFormNew($request, $this->form);

        $this->setTemplate('new');
    }

    public function executeEdit(sfWebRequest $request) {
        $this->forward404Unless($ruta = Doctrine_Core::getTable('ruta')->find(array($request->getParameter('id_ru'))), sprintf('Object ruta does not exist (%s).', $request->getParameter('id_ru')));
        $this->idco=$ruta->getIdCo();
        $this->form = new rutaForm($ruta);
    }

    public function executeUpdate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
        $this->forward404Unless($ruta = Doctrine_Core::getTable('ruta')->find(array($request->getParameter('id_ru'))), sprintf('Object ruta does not exist (%s).', $request->getParameter('id_ru')));
        $this->form = new rutaForm($ruta);

        $this->processFormEdit($request, $this->form);

        $this->setTemplate('edit');
    }

    public function executeDelete(sfWebRequest $request) {
        $request->checkCSRFProtection();

        $this->forward404Unless($ruta = Doctrine_Core::getTable('ruta')->find(array($request->getParameter('id_ru'))), sprintf('Object ruta does not exist (%s).', $request->getParameter('id_ru')));
        $aux=$ruta->getIdCo();
        $ruta->delete();
        $this->getUser()->setFlash('notice', 'Ruta eliminada exitosamente');
        $this->redirect('rutas/index?id_co='.$aux);
    }

    
    protected function processFormEdit(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {                        
            $ruta = $form->save();
            $this->getUser()->setFlash('notice', 'Ruta seleccionada editada exitosamente');
            $this->redirect('rutas/index?id_co=' . $ruta->getIdCo());
        }
    }
    
    protected function processFormNew(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {            
            $form->getObject()->setIdCo($this->getUser()->getAttribute('id_co'));            
            $ruta = $form->save();
            $ruta->setActivo(true);
            $ruta->save();
            $this->getUser()->setFlash('notice', 'Ruta creada satisfactoriamente');
            $this->redirect('rutas/index?id_co=' . $ruta->getIdCo());
            
            
            
        }
    }

}